package com.ouheng.shop.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ouheng.common.annotation.Log;
import com.ouheng.common.domain.Tree;
import com.ouheng.common.utils.PageUtils;
import com.ouheng.common.utils.Query;
import com.ouheng.common.utils.R;
import com.ouheng.shop.domain.ProductDO;
import com.ouheng.shop.domain.RushPurchaseDO;
import com.ouheng.shop.service.ProductService;
import com.ouheng.shop.service.RushPurchaseProductService;
import com.ouheng.shop.service.RushPurchaseService;

/**
 * 抢购
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-08-30 09:33:07
 */
 
@Controller
@RequestMapping("/shop/rushPurchase")
public class RushPurchaseController {
	@Autowired
	private RushPurchaseService rushPurchaseService;
	
	@Autowired
	private RushPurchaseProductService rushPurchaseProductService;
	
	@Autowired
	private ProductService productService;
	
	@GetMapping()
	@RequiresPermissions("shop:rushPurchase:rushPurchase")
	String RushPurchase(){
	    return "shop/rushPurchase/rushPurchase";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shop:rushPurchase:rushPurchase")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<RushPurchaseDO> rushPurchaseList = rushPurchaseService.list(query);
		int total = rushPurchaseService.count(query);
		PageUtils pageUtils = new PageUtils(rushPurchaseList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("shop:rushPurchase:add")
	String add(){
	    return "shop/rushPurchase/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("shop:rushPurchase:edit")
	String edit(@PathVariable("id") Long id,Model model){
		RushPurchaseDO rushPurchase = rushPurchaseService.get(id);
		model.addAttribute("rushPurchase", rushPurchase);
	    return "shop/rushPurchase/edit";
	}
	
	/**
	 * 保存
	 */
	@Log("添加抢购设置")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:rushPurchase:add")
	public R save( RushPurchaseDO rushPurchase){
		if(rushPurchaseService.save(rushPurchase)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@Log("编辑抢购设置")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:rushPurchase:edit")
	public R update( RushPurchaseDO rushPurchase){
		rushPurchaseService.update(rushPurchase);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@Log("删除抢购设置")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:rushPurchase:remove")
	public R remove( Long id){
		if(rushPurchaseService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@Log("批量删除抢购设置")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:rushPurchase:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		rushPurchaseService.batchRemove(ids);
		return R.ok();
	}
	
//	@GetMapping("/relevance/{id}")
//	@RequiresPermissions("shop:rushPurchase:prorelevance")
//	String relevance(@PathVariable("id") Long id,Model model) {
//		RushPurchaseDO rushPurchase = rushPurchaseService.get(id);
//		model.addAttribute("rushPurchase", rushPurchase);
//		return "shop/rushPurchase/relevance";
//	}
	
	@GetMapping("/protree/{pid}")
	@ResponseBody
	@RequiresPermissions("shop:rushPurchase:prorelevance")
	Tree<ProductDO> protree(@PathVariable("pid") Long pid) {
		Tree<ProductDO> tree = rushPurchaseService.getTree(pid);
		return tree;
	}
	
//	@Log("保存关联商品")
//	@ResponseBody
//	@PostMapping("/saveRelevance")
//	public R saveRelevance(RushPurchaseDO rushPurchase){
//		rushPurchaseService.saveRelevance(rushPurchase);
//		return R.ok();
//	}
	
	
	@GetMapping("/relevance/{id}")
	String relevance(@PathVariable("id")Long id,Model model){
		List<Long> ppids = rushPurchaseProductService.listProductidByPid(id);
		model.addAttribute("ppids", ppids);
		model.addAttribute("pid", id);
	    return "shop/rushPurchase/relevance";
	}
	
	@ResponseBody
	@GetMapping("/listrelevance")
	public List<ProductDO> listrelevance(@RequestParam Map<String, Object> params){
		//查询列表数据
		List<ProductDO> productList = productService.list(params);
		return productList;
	}
	
	@Log("保存关联商品")
	@ResponseBody
	@PostMapping("/saveRelevance")
	public R saveRelevance( Long pid,@RequestParam("ppids[]")Long[] ppids){
		rushPurchaseService.saveRelevance(pid,ppids);
		return R.ok();
	}
	
}
